;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Stock.Restore", [])
        .controller("RestoreCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.activeFlag = [true, false];
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };

            $scope.condition = {};
            $scope.dataList = [];
            $scope.tbConf = {
                fixedWidth: 100,
                scrollWidth: 1070,
                tableCol: [
                    {
                        type: "",
                        name: "拆件恢复单号",
                        dataField: "restoreOrder",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        clickFn: "viewDetail",
                    },
                    {
                        type: "",
                        name: "拆件单号",
                        dataField: "splitOrder",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "总数量",
                        dataField: "amount",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "已恢复数量",
                        dataField: "restoreAmount",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "恢复状态",
                        dataField: "restoreStatusStr",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "创建人",
                        dataField: "createName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "创建时间",
                        dataField: "createDateStr",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 400,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };


            //详情零件原件列表配置项
            $scope.detailOrigTbConf = {
                scrollWidth: 1100,
                tableCol: [
                    {
                        name: "零件条码",
                        width: 140,
                        dataField: "barcode"
                    },
                    {
                        name: "零件编号",
                        width: 120,
                        dataField: "materialsNo"
                    },
                    {
                        name: "零件中文名",
                        width: 130,
                        dataField: "materialsDes"
                    },
                    {
                        name: "零件类型",
                        width: 120,
                        dataField: "materialsType"
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit"
                    },
                    {
                        name: "数量",
                        width: 100,
                        dataField: "amount"
                    },
                    {
                        name: "是否恢复",
                        width: 100,
                        dataField: "isRestore"
                    },
                    {
                        name: "恢复人",
                        width: 120,
                        dataField: "restoreName"
                    },
                    {
                        name: "恢复时间",
                        width: 120,
                        dataField: "restoreDateShow"
                    }
                ]
            };

            //详情页面映射字段
            $scope.detailMap = [
                {
                    name: '拆件恢复单号',
                    field: 'restoreOrder'
                },
                {
                    name: '拆件单号',
                    field: 'splitOrder'
                },
                {
                    name: '总数量',
                    field: 'amount'
                },
                {
                    name: '已恢复数量',
                    field: 'restoreAmount'
                },
                {
                    name: '创建人',
                    field: 'createName'
                },
                {
                    name: '创建时间',
                    field: 'createDateShow'

                }
            ];

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
            };

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    fixedWidth: {left: $scope.tbConf.fixedWidth + "px"}
                };
            };

            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/restore/export"),
                sectionParam: {ids: []},
                allUrl: ybHttp.RequestUrl("device", "/restore/exportAll")
            };

            var btnPermissionCode = {
                restoreExport: window.ActionCode.restoreExport,
                restorePrint:window.ActionCode.restorePrint
            };

            $scope.btnPermission = {
                sapErrorExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }
            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };

            //打印按钮
            $scope.printBill = function (item) {
                var id = item.id;
                var billIds = [];
                billIds.push(id);
                var param = {
                    billId: billIds,
                    printType: "RestorePrint"
                };
                ybWidget.SetIsPrintOrPreview(param);
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/restore/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    translateField();
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };


            function viewDetail(item) {
                getDataDetail(item.id).then(function (value) {
                    if (value) {
                        $scope.detailObj = value.value;
                        $scope.changeTab(1);
                        $scope.detailObj.originalDetailList = $scope.detailObj.materialList;

                        angular.forEach($scope.detailObj.originalDetailList, function (item) {
                            item.isRestore = item.restoreStatus===0?"未恢复":"已恢复";
                            item.restoreDateShow = $filter("YbDate")(item.restoreDate, "yyyy-MM-dd HH:mm:ss");
                        });

                        $scope.detailObj.headerInfo.createDateShow = $filter("YbDate")($scope.detailObj.headerInfo.createDate, "yyyy-MM-dd HH:mm:ss");
                        $scope.detailObj.headerInfo.splitTypeShow = $filter("DismantlingType")($scope.detailObj.headerInfo.splitType);
                        $scope.detailObj.headerInfo.splitStatusShow = $filter("DismantlingStatus")($scope.detailObj.headerInfo.splitStatus);
                        $scope.detailObj.headerInfo.restoreStatusShow = $filter("RestoreStatus")($scope.detailObj.headerInfo.restoreStatus);
                    }
                })
            }


            var getDataDetail = function (id) {
                var service = "device";
                var reqUrl = "/restore/item";

                return ybHttp.Get(service, reqUrl, {id: id}).then(function (resp) {

                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    return resp;
                });
            };

            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            $scope.selectAll = function (list, type) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
                }
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item, list, type) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    if (!checkAll(list)) {
                        selectAll = false;
                    } else {
                        selectAll = true;
                    }
                }
                if (angular.isDefined(type)) {
                    $scope.markInfo["allChecked" + type] = selectAll;
                } else {
                    $scope.markInfo.allChecked = selectAll;
                }
                getBillIds();
            };

            //翻译字段
            function translateField() {
                angular.forEach($scope.dataList, function (item) {
                    item.restoreStatusStr = $filter('RestoreStatus')(item.restoreStatus);
                    item.createDateStr = $filter('YbDate')(item.createDate, "yyyy-MM-dd HH:mm:ss");
                });
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);